package com.itheima.reggie.controller;


import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

// 收货地址模块
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    // 查询收货地址列表
    @GetMapping("/address/list")
    public ResultInfo findList() {
        // 1.调用service查询
        List<Address> addressList = addressService.findList();
        // 2.返回查询结果
        return ResultInfo.success(addressList);
    }

    //新增地址
    @PostMapping("/address")
    public ResultInfo add(@RequestBody Address address){
        // 2.调用service新增
        addressService.add(address);
        // 3.返回成功
        return ResultInfo.success();
    }


    // 设置默认收货地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long> param){
        //取出地址id
        Long id = param.get("id");
        //service设置默认
        addressService.setDefault(id);
        //返回成功
        return ResultInfo.success();
    }

    // 查询默认收货地址
    @GetMapping("/address/default")
    public ResultInfo findDefault() {
        // 1.调用service查询默认地址
        Address address = addressService.findDefault();
        // 2.返回结果
        return ResultInfo.success(address);
    }

    //回显收货地址
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        //调用service查询
        Address address = addressService.findById(id);
        //返回结果
        return ResultInfo.success(address);
    }

    //修改收货地址
    @PutMapping("/address")
    public ResultInfo update(@RequestBody Address address){
        //调用service更改
        addressService.update(address);
        //返回
        return  ResultInfo.success();
    }


}
